from typing import Any

import time
import hashlib
import urllib.parse
from decimal import Decimal
import base64
import re
from dify_plugin import ToolProvider
from dify_plugin.errors.tool import ToolProviderCredentialValidationError


class PayProvider(ToolProvider):

    def get_key_and_value_str(params):
        try:

        except Exception as e:
            return None

    def _validate_credentials(self, credentials: dict[str, Any]) -> None:
        base_url = "https://www.cuupay.com/submit"
        try:

            payload = {
                "uid": credentials.get("uid"),
                "key": credentials.get("api_key"),
                "apiKey": credentials.get("api_key"),
                "trade_name": "测试订单",
                "remarks": "测试描述",
                "time": int(time.time()),
                "mod": "api",
                "mod": "alipay",
                "third_trade_no": str(credentials.get("uid")) + str(int(time.time() * 1000)),
                "notify_url": credentials.get("notify_url"),
                "return_url": "https://www.cuupay.com/login/qqlogin/paySuccess",
                "money": Decimal(100)
            }

            sorted_items = sorted(payload.items(), key=lambda x: x[0])
            param_str = ""
            for key, value in sorted_items:
                if key is not None and key != "":
                    if value is not None and value != "":
                        param_str += f"{key}={value}&"
            sign_str = param_str[:-1]
            # 生成签名并移除key
            payload["sign"] = hashlib.md5(sign_str.encode('utf-8')).hexdigest()
            payload.pop("key", None)
            encoded_params = urllib.parse.urlencode(payload)
            full_url = f"{base_url}?{encoded_params}"
            return full_url
        except Exception as e:
            raise ToolProviderCredentialValidationError(str(e))

